package secondriver.heathfood.common;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Message;
import secondriver.heathfood.HeathFoodApplication;
import secondriver.heathfood.bean.ApkInfoEntity;
import secondriver.heathfood.client.ApiClient;
import secondriver.heathfood.client.Constants;
import secondriver.heathfood.bean.PageParams;
import secondriver.heathfood.util.FileUtil;
import secondriver.heathfood.util.LogUtil;

/**
 * Author : secondriver
 * Date :  2015/10/22
 */
public class BackgroundTasks {

    public static class LoadFoodClass extends DefaultLoadDataBackgroundTask {

        public LoadFoodClass(ApiClient apiClient) {
            super(apiClient);
        }

        @Override
        public void executeTask(Bundle bundle, Message message) {
            long id = bundle.getLong(Constants.CLASSIFY_ID);
            message.obj = apiClient.getFoodClassContent(id);
            message.sendToTarget();
        }
    }

    public static class LoadFoodBrief extends DefaultLoadDataBackgroundTask {

        public LoadFoodBrief(ApiClient apiClient) {
            super(apiClient);
        }

        @Override
        public void executeTask(Bundle bundle, Message message) {
            PageParams pageParams = bundle.getParcelable(Constants.FOOD_LIST);
            message.obj = apiClient.getFoodBriefForPage(pageParams.cid,
                    pageParams.page, pageParams.rows);
            message.sendToTarget();
        }
    }

    public static class LoadFoodDetail extends DefaultLoadDataBackgroundTask {

        public LoadFoodDetail(ApiClient apiClient) {
            super(apiClient);
        }

        @Override
        public void executeTask(Bundle bundle, Message message) {
            long id = bundle.getLong(Constants.FOOD_ID);
            message.obj = apiClient.getFoodDetailById(id);
            message.sendToTarget();
        }
    }

    public static abstract class DefaultLoadDataBackgroundTask implements BackgroundTaskExecutor.BackgroundTask {

        final ApiClient apiClient;

        public DefaultLoadDataBackgroundTask(ApiClient apiClient) {
            this.apiClient = apiClient;
        }

    }

    public static class PullAppVersionInfoTask implements BackgroundTaskExecutor.BackgroundTask {

        private final Context context;

        private final LogUtil logUtil = LogUtil.getLogger(PullAppVersionInfoTask.class.getName());

        public PullAppVersionInfoTask(Context context) {
            this.context = context;
        }

        @Override
        public void executeTask(Bundle bundle, Message message) {
            if (null == bundle) {
                return;
            }
            String appVersionUrl = bundle.getString(HeathFoodApplication.META_KEY_GET_APP_VERSION_URL);
            FileUtil.deleteFile(ApkInfoEntity.APK_INFO_FILE_NAME, context);
            boolean appVersionResult = false;
            if (null != appVersionUrl) {
                String appVersionContent = ApiClient.getContent(appVersionUrl);
                logUtil.info("AppVersionContent = %s", appVersionContent);
                if (null != appVersionContent && !"".equals(appVersionContent)) {
                    FileUtil.writeToFile(ApkInfoEntity.APK_INFO_FILE_NAME, appVersionContent, context);
                    appVersionResult = true;
                } else {
                    logUtil.error("App version content from server is empty !");
                }
            } else {
                logUtil.error("Application meta data key=%s, value is null !", HeathFoodApplication.META_KEY_GET_APP_VERSION_URL);
            }
            if (message != null) {
                message.obj = appVersionResult;
                message.sendToTarget();
            }
        }
    }
}
